/**
 * 
 */
package org.whuims.indritools.optmizing;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.StringUtils;
import org.whuims.common.proper.Config;

/**
 * @author ChengQikai
 * @version: 1.0
 */
public class QueryIniFactory {
	public static final Integer STEM_PORTER = 0;
	public static final Integer STEM_KROVETZ = 1;
	private static String head = "<parameters>";
	private static String tail = "<trecFormat>true</trecFormat>\r\n<queryOffset>1</queryOffset>\r\n<runID>run</runID>\r\n</parameters>";
	private String rule = "<rule>method:dir</rule>";
	private int count = 1000;
	private int stemMode = 0;
	private boolean train = false;
	public static Map<String, String> queryMap = new HashMap<String, String>();

	public QueryIniFactory(String rule, int stemMode, boolean train, int count) {
		super();
		this.rule = rule;
		this.stemMode = stemMode;
		this.count = count;
		this.train = train;
	}

	static {
		String path = Config.getProp("query_path");
		try {
			List<String> list = FileUtils.readLines(new File(path));
			for (String s : list) {
				s = s.trim();
				if (s.startsWith("<query>")) {
					String queryNumber = StringUtils.substringsBetween(s,
							"<query><number>", "</number>")[0];
					queryMap.put(queryNumber, s);
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * makeQuery
	 * 
	 * @return
	 */
	public String makeQuerys() {
		StringBuilder sb = new StringBuilder();
		makeHead(sb);

		for (int i = 101; i < 151; i++) {
			sb.append(queryMap.get("R" + i)).append("\r\n");
		}

		makeTail(sb);
		return sb.toString();
	}

	/**
	 * @param sb
	 */
	private void makeTail(StringBuilder sb) {
		sb.append(tail);
	}

	/**
	 * @param sb
	 */
	private void makeHead(StringBuilder sb) {
		sb.append(head).append("\r\n");
		sb.append("<index>");
		if (this.stemMode == QueryIniFactory.STEM_PORTER) {
			if (this.train) {
				sb.append(Config.getProp("index_porter_train"));
			} else {
				sb.append(Config.getProp("index_porter_test"));
			}
		} else {
			if (this.train) {
				sb.append(Config.getProp("index_krovetz_train"));
			} else {
				sb.append(Config.getProp("index_krovetz_test"));
			}
		}
		sb.append("</index>\r\n");
		sb.append(this.rule).append("\r\n");
		sb.append("<count>").append(count).append("</count>").append("\r\n");
		try {
			List<String> stopwords=FileUtils.readLines(new File("resource/smart_stopwords"));
			sb.append("<stopper>\r\n");
			for(String s:stopwords){
				sb.append("\t<word>").append(s).append("</word>\r\n");
			}
			sb.append("</stopper>\r\n");
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		
	}

	public static void main(String[] args) {
//		for (int i = 101; i < 151; i++) {
//			String str = "R" + i;
//			System.out.println(QueryIniFactory.queryMap.get(str));
//		}
		RuleFactory rf=new RuleFactory();
		rf.initLM_Dir_Rules();
		QueryIniFactory ft = new QueryIniFactory(rf.rules.get(0), 0, true, 1000);
		System.out.println(ft.makeQuerys());
	}

}
